{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-09-20T11:57:12.242547Z",
     "start_time": "2024-09-20T11:57:10.503155Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "df_train = pd.read_csv(r'D:\\pcdaima\\shixun\\shixun1\\data\\fugou\\train.csv')\n",
    "df_test = pd.read_csv(r'D:\\pcdaima\\shixun\\shixun1\\data\\fugou\\test.csv')\n",
    "df_test"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         user_id  merchant_id  prob  age_range  gender  item_id  cat_id  \\\n",
       "0         163968         4605   NaN        0.0     0.0   772645    1368   \n",
       "1         163968         4605   NaN        0.0     0.0   772645    1368   \n",
       "2         360576         1581   NaN        2.0     2.0   948181     614   \n",
       "3         360576         1581   NaN        2.0     2.0  1111020     614   \n",
       "4         360576         1581   NaN        2.0     2.0   294442     614   \n",
       "...          ...          ...   ...        ...     ...      ...     ...   \n",
       "1792407    32639         3536   NaN        0.0     0.0   264956     946   \n",
       "1792408    32639         3536   NaN        0.0     0.0  1106697     946   \n",
       "1792409    32639         3319   NaN        0.0     0.0   179481     898   \n",
       "1792410    32639         3319   NaN        0.0     0.0   179481     898   \n",
       "1792411    32639         3319   NaN        0.0     0.0   179481     898   \n",
       "\n",
       "         brand_id  time_stamp  action_type  \n",
       "0          7622.0        1111            2  \n",
       "1          7622.0        1111            0  \n",
       "2          4066.0        1111            2  \n",
       "3          4066.0        1111            2  \n",
       "4          4066.0        1111            2  \n",
       "...           ...         ...          ...  \n",
       "1792407    3481.0        1111            0  \n",
       "1792408    3481.0        1111            2  \n",
       "1792409    5508.0        1110            0  \n",
       "1792410    5508.0        1111            0  \n",
       "1792411    5508.0        1111            2  \n",
       "\n",
       "[1792412 rows x 10 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>prob</th>\n",
       "      <th>age_range</th>\n",
       "      <th>gender</th>\n",
       "      <th>item_id</th>\n",
       "      <th>cat_id</th>\n",
       "      <th>brand_id</th>\n",
       "      <th>time_stamp</th>\n",
       "      <th>action_type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>163968</td>\n",
       "      <td>4605</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>772645</td>\n",
       "      <td>1368</td>\n",
       "      <td>7622.0</td>\n",
       "      <td>1111</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>163968</td>\n",
       "      <td>4605</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>772645</td>\n",
       "      <td>1368</td>\n",
       "      <td>7622.0</td>\n",
       "      <td>1111</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>360576</td>\n",
       "      <td>1581</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>948181</td>\n",
       "      <td>614</td>\n",
       "      <td>4066.0</td>\n",
       "      <td>1111</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>360576</td>\n",
       "      <td>1581</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1111020</td>\n",
       "      <td>614</td>\n",
       "      <td>4066.0</td>\n",
       "      <td>1111</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>360576</td>\n",
       "      <td>1581</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>294442</td>\n",
       "      <td>614</td>\n",
       "      <td>4066.0</td>\n",
       "      <td>1111</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1792407</th>\n",
       "      <td>32639</td>\n",
       "      <td>3536</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>264956</td>\n",
       "      <td>946</td>\n",
       "      <td>3481.0</td>\n",
       "      <td>1111</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1792408</th>\n",
       "      <td>32639</td>\n",
       "      <td>3536</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1106697</td>\n",
       "      <td>946</td>\n",
       "      <td>3481.0</td>\n",
       "      <td>1111</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1792409</th>\n",
       "      <td>32639</td>\n",
       "      <td>3319</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>179481</td>\n",
       "      <td>898</td>\n",
       "      <td>5508.0</td>\n",
       "      <td>1110</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1792410</th>\n",
       "      <td>32639</td>\n",
       "      <td>3319</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>179481</td>\n",
       "      <td>898</td>\n",
       "      <td>5508.0</td>\n",
       "      <td>1111</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1792411</th>\n",
       "      <td>32639</td>\n",
       "      <td>3319</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>179481</td>\n",
       "      <td>898</td>\n",
       "      <td>5508.0</td>\n",
       "      <td>1111</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1792412 rows × 10 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-20T11:57:12.568495Z",
     "start_time": "2024-09-20T11:57:12.243548Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import roc_auc_score\n",
    "X = df_train.drop('label',axis=1)\n",
    "y = df_train['label']\n",
    "df_test.shape"
   ],
   "id": "418a0dbd5c706c31",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1792412, 10)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-20T11:59:20.671203Z",
     "start_time": "2024-09-20T11:57:12.569497Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.tree import DecisionTreeClassifier\n",
    "import xgboost as xgb\n",
    "from sklearn.feature_selection import SelectKBest\n",
    "#选择最高权重的5个特征\n",
    "best = SelectKBest(k=5)\n",
    "X_new = best.fit_transform(X,y)\n",
    "\n",
    "X_train,X_test,y_train,y_test = train_test_split(X_new,y,train_size=0.8,random_state=42)\n",
    "model = xgb.XGBClassifier(\n",
    "    max_depth=8,\n",
    "    n_estimators=3000,\n",
    "    min_child_weight=100, \n",
    "    colsample_bytree=0.8, \n",
    "    subsample=0.8, \n",
    "    eta=0.3,    \n",
    "    seed=42   \n",
    ")\n",
    "model.fit(X_train,y_train)\n",
    "y_scores = model.predict_proba(X_test)[:, 1]  # 获取正类的概率\n",
    "auc1 = roc_auc_score(y_test,y_scores)\n",
    "auc1"
   ],
   "id": "eb6e4a32837e03d6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7678193988606268"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-20T11:59:20.678148Z",
     "start_time": "2024-09-20T11:59:20.672203Z"
    }
   },
   "cell_type": "code",
   "source": "X_new",
   "id": "585794652ad56fa9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3.906e+03, 6.000e+00, 0.000e+00, 1.110e+03, 0.000e+00],\n",
       "       [3.906e+03, 6.000e+00, 0.000e+00, 1.031e+03, 3.000e+00],\n",
       "       [3.906e+03, 6.000e+00, 0.000e+00, 1.031e+03, 3.000e+00],\n",
       "       ...,\n",
       "       [4.140e+03, 4.000e+00, 2.000e+00, 1.110e+03, 0.000e+00],\n",
       "       [4.140e+03, 4.000e+00, 2.000e+00, 1.110e+03, 0.000e+00],\n",
       "       [4.140e+03, 4.000e+00, 2.000e+00, 1.110e+03, 0.000e+00]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-20T11:59:20.682109Z",
     "start_time": "2024-09-20T11:59:20.680150Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "2dc6b23e2410cdcd",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-20T12:01:06.154685Z",
     "start_time": "2024-09-20T11:59:20.683112Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.ensemble import RandomForestClassifier\n",
    "model1 = RandomForestClassifier(max_depth=10, random_state=0,class_weight='balanced')\n",
    "model1.fit(X_train,y_train)\n",
    "y_pred1=model1.predict(X_test)\n",
    "y_proba1 = model1.predict_proba(X_test)\n",
    "# print('模型的评估报告：\\n',classification_report(y_test, y_pred1))\n",
    "y_scores = model1.predict_proba(X_test)[:, 1]  # 获取正类的概率\n",
    "auc1 = roc_auc_score(y_test,y_scores)\n",
    "auc1"
   ],
   "id": "1e73bea117ff834a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6613889740785377"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "jupyter": {
     "is_executing": true
    },
    "ExecuteTime": {
     "start_time": "2024-09-20T12:01:06.155687Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.model_selection import GridSearchCV\n",
    "xgb_pred = {\n",
    "    'max_depth':[4,6,8],\n",
    "    'min_child_weight':[100,200,300],\n",
    "    'colsample_bytree':[0.4,0.6,0.8]\n",
    "}\n",
    "xgb_grid = GridSearchCV(model,xgb_pred,cv=5)\n",
    "xgb_grid.fit(X_train,y_train)\n",
    "best_xgbmodel = xgb_grid.best_estimator_\n",
    "best_proba = best_xgbmodel.predict_proba(X_test)[:,1]\n",
    "auc1 = roc_auc_score(y_test,best_proba)\n",
    "auc1"
   ],
   "id": "27b7fbd932fe9a9c",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception ignored on calling ctypes callback function: <bound method DataIter._next_wrapper of <xgboost.data.SingleBatchInternalIter object at 0x000002358CBA4AA0>>\n",
      "Traceback (most recent call last):\n",
      "  File \"D:\\ANACONDA\\Lib\\site-packages\\xgboost\\core.py\", line 582, in _next_wrapper\n",
      "    def _next_wrapper(self, this: None) -> int:  # pylint: disable=unused-argument\n",
      "\n",
      "KeyboardInterrupt: \n"
     ]
    }
   ],
   "execution_count": null
  },
  {
   "metadata": {
    "jupyter": {
     "is_executing": true
    }
   },
   "cell_type": "code",
   "source": "xgb_grid.best_params_",
   "id": "f509af818e2ce7cd",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
